package com.example;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Description:服务器获取请求数据
 * User: bjc
 * Date: 2025-05-30
 * Time: 20:10
 */
@RestController
public class RequestController {
    @RequestMapping("/request")
    public String request(HttpServletRequest request){
        //1.获取请求方式
        String method = request.getMethod();
        System.out.println("method = " + method);

        //2.获取请求URL路径
        String url = request.getRequestURL().toString();//获取完整请求路径
        System.out.println("url = " + url);

        String uri = request.getRequestURI();//获取资源访问路径
        System.out.println("uri = " + uri);

        //3.获取请求协议
        String protocol = request.getProtocol();
        System.out.println("protocol = " + protocol);

        //4.获取请求参数 name, age
        String name = request.getParameter("name");//传递请求参数的名字
        String age = request.getParameter("age");
        System.out.println("name = " + name + ", age = " + age);

        //5.获取请求头 Accept
        String accept = request.getHeader("Accept");
        System.out.println("accept = " + accept);

        return "request success";
    }

}
